
const path = require('path');
const fs = require('fs');

export default   class  NodeRoot {



    constructor(rootPath){
        this.cache={}; //模块
        this.cacheConfig={} ;
        this.rootPath=rootPath;
    }
    //直接从缓存中获取数据
    findCacheData(cacheKey){
        let _this=this;
        return  _this.cache[cacheKey]
    }
    //获取模块
    findData(moduleName,dir,fsName,loadName ){
        let _this=this;
        let configCache = this.cacheConfig[loadName]
        if(!configCache){
            // let cacheKey=configCache.findKey(moduleName,dir,fsName,loadName);
            return new Promise(function (resolve, reject) {
                if(_this.cache[moduleName]){

                    resolve(_this.cache[moduleName]);
                }else {
                    let res= _this.load(moduleName,dir,fsName,loadName )

                    res.then(resData =>{
                        _this.cache[moduleName]=resData;

                        resolve(resData);
                    })
                }
            });
        }else {
            return new Promise(function (resolve, reject) {
                let res= _this.load(moduleName,dir,fsName,loadName)

                res.then(resData =>{

                    resolve(resData);
                })
            });
        }
    }

    configCache(configCache,dir,fsName,loadName){
        this.cacheConfig[loadName]=configCache;
    }
      load(dir){
        let _this=this;
          return new Promise(function (resolve, reject) {
             let buff= fs.readFileSync (path.join(_this.rootPath,dir));
              resolve(buff)
          });
    }


}

